<template>
  <div class="app-container">
    <ht-search-form
      :defaultEventKey="defaultEventKey"
      :dictMap="dictMap"
      :queryParams="queryParams"
      :outputQueryParams="queryParams"
      :formOptions="formOptions"
      :advOptions="advOptions"
      permission="batch:missionManage:view"
    ></ht-search-form>
    <ht-table
      :defaultEventKey="defaultEventKey"
      ref="dataTable"
      :multiSelect="false"
      :dictMap="dictMap"
      :reqConfig="reqConfig"
      :queryParams="queryParams"
      :tableBindColumns="tableBindColumns"
    >
      <template v-slot:flowContext="slotProps">
        <el-popover
          v-if="slotProps.row.flowContext"
          placement="top-start"
          width="300"
          trigger="hover"
          :content="slotProps.row.flowContext"
        >
          <span slot="reference">{{ $t("common.check") }}</span>
        </el-popover>
      </template>
      <template v-slot:operate>
        <el-table-column
          :label="$t('common.operate')"
          align="left"
          width="150"
          fixed="right"
        >
          <template slot-scope="scope">
            <ht-button-group
              :buttonList="[
                {
                  label: $t('common.check'),
                  permission: 'batch:missionManage:details',
                  doFunc: () => {
                    handleAction('details', scope.row);
                  },
                },
                {
                  label: $t('batch.missionManage.form.reRun'),
                  permission: 'batch:missionManage:reRun',
                  doFunc: () => {
                    handleAction('reRun', scope.row);
                  },
                },
                {
                  label: $t('common.cancel'),
                  permission: 'batch:missionManage:cancel',
                  visible: scope.row.status != 3 && scope.row.status != 4,
                  doFunc: () => {
                    handleAction('cancel', scope.row);
                  },
                },
              ]"
            />
          </template>
        </el-table-column>
      </template>
    </ht-table>
    <mission-details ref="missionDetails"></mission-details>
  </div>
</template>
<script>
import { apiUrlConfig, cancel, flowReRun } from "@/api/batch_app/missionManage";
import { listPageMixin } from "@/libs/listPageMixin";
import MissionDetails from "@/views/batch/missionMange/components/MissionDetails";

export default {
  name: "MissionManage",
  components: { MissionDetails },
  mixins: [listPageMixin],
  data() {
    return {
      defaultEventKey: "missionManager",
      // 查询参数
      queryParams: {
        flowCode: "",
        flowName: "",
        status: "",
      },
      // 搜索
      formOptions: [],
      advOptions: [],
      reqConfig: apiUrlConfig,
      // 列信息
      tableBindColumns: [
        { label: `ID`, prop: "id" },
        {
          label: this.$t("batch.missionManage.form.processName"),
          prop: "flowName",
        },
        {
          label: this.$t("batch.missionManage.form.processCode"),
          prop: "flowCode",
        },
        //{ label: `流程标识`, prop: "flowFlag", dictCode: 'scubb_flag_type' },
        {
          label: this.$t("batch.missionManage.form.triggerType"),
          prop: "triggerType",
          dictCode: "scubb_trigger_type",
        },
        // { label: `预期执行事件`, prop: "createTime",width: 120 },
        {
          label: this.$t("batch.missionManage.form.flowContext"),
          prop: "flowContext",
          customSlot: true,
        },
        {
          label: this.$t("batch.missionManage.form.nodeCount"),
          prop: "nodeCount",
        },
        {
          label: this.$t("batch.missionManage.form.endNodeCount"),
          prop: "endNodeCount",
        },
        // { label: `失败数`, prop: "updateTime" },
        {
          label: this.$t("batch.missionManage.form.startTime"),
          prop: "createTime",
        },
        {
          label: this.$t("batch.missionManage.form.updateTime"),
          prop: "updateTime",
        },
        {
          label: this.$t("batch.missionManage.form.status"),
          prop: "status",
          dictCode: "scubb_flow_status",
        },
      ],
    };
  },
  mounted() {
    const formOptions = [
      {
        type: "input",
        prop: "flowName",
        defaultAttr: {
          label: this.$t("batch.missionManage.form.processName"),
          placeholder: this.$t("batch.missionManage.rules.pleaseEnter"),
        },
      },
      {
        type: "input",
        prop: "flowCode",
        defaultAttr: {
          label: this.$t("batch.missionManage.form.processCode"),
          placeholder: this.$t("batch.missionManage.rules.pleaseEnter"),
        },
      },
      {
        type: "dict",
        dict: "scubb_flow_status",
        prop: "status",
        defaultAttr: {
          label: this.$t("batch.missionManage.form.status"),
          placeholder: this.$t("batch.missionManage.rules.pleaseSelect"),
        },
      },
    ];

    const advOptions = [
      {
        type: "dates",
        nameObj: {
          startTimeName: "beginTime",
          endTimeName: "endTime",
        },
        defaultAttr: {
          size: "small",
          label: this.$t("batch.missionManage.form.creationDate"),
          placeholder: [
            this.$t("batch.missionManage.form.selectStartDate"),
            this.$t("batch.missionManage.form.selectEndDate"),
          ],
          format: "yyyy-MM-dd",
          valueFormat: "yyyy-MM-dd",
        },
      },
    ];
    this.formOptions = formOptions;
    this.advOptions = advOptions;
  },
  methods: {
    /***
     * 按钮操作汇总
     */
    handleAction(type, row = {}) {
      switch (type) {
        case "details":
          this.$refs.missionDetails.show(row.id, row.flowCode);
          break;
        case "reRun":
          flowReRun(row.flowCode, row.requestId).then((res) => {
            if (res.result) {
              this.$message.success(this.$t("common.operateSuccess"));
            } else {
              this.$message.error(this.$t("common.failure"));
            }
          });
          break;
        case "cancel":
          this.$confirm(
            this.$t("batch.missionManage.rules.cancelTask"),
            this.$t("common.warning"),
            {
              confirmButtonText: this.$t("common.confirm"),
              cancelButtonText: this.$t("common.cancel"),
              type: "warning",
            }
          )
            .then(function () {
              return cancel(row.id);
            })
            .then(() => {
              this.doQuery(this.defaultEventKey);
              this.msgSuccess(this.$t("common.cancelSuccess"));
            })
            .catch((action) => {});
          break;
      }
    },
  },
};
</script>
